<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-tabs v-model="tab" @tab-click="getList(1)">
          <el-tab-pane
            :label="item.name"
            :name="item.type.toString()"
            v-for="(item, index) in headeTab"
            :key="index"
          />
        </el-tabs>
        <div class="container">
          <el-form
            inline
            size="small"
            :model="shopFrom"
            ref="shopFrom"
            label-width="100px"
          >
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                <el-col :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                  <el-form-item label="关键字：">
                    <el-input
                      v-model="shopFrom.name"
                      placeholder="请输入"
                      clearable
                      class="selWidth"
                    />
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col
                :xs="24"
                :sm="24"
                :md="24"
                :lg="6"
                :xl="6"
                class="text-right shopFrom"
              >
                <el-form-item>
                  <el-button
                    type="primary"
                    icon="ios-search"
                    label="default"
                    class="mr15"
                    size="small"
                    @click="searchs"
                    >搜索</el-button
                  >
                  <el-button
                    class="ResetSearch mr10"
                    @click="reset('shopFrom')"
                    size="small"
                    >重置</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <el-button
          class="mr10"
          type="primary"
          size="small"
          @click="editUser(null)"
          >添加门店</el-button
        >
      </div>
      <el-table
        ref="table"
        v-loading="loading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        highlight-current-row
      >
        <el-table-column prop="id" label="ID" min-width="80" />
        <el-table-column label="门店名称" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.name | filterEmpty }} </span>
          </template>
        </el-table-column>
        <el-table-column label="门店电话" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.telephone | filterEmpty }} </span>
          </template>
        </el-table-column>
        <el-table-column label="门店地址" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.address | filterEmpty }} </span>
          </template>
        </el-table-column>
        <el-table-column label="付款二维码" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.imageUrl"
                :preview-src-list="[scope.row.imageUrl]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="营业时间" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.dayTime | filterEmpty }} </span>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" min-width="150">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-text="显示"
              inactive-text="隐藏"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="130"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope" v-if="tab !== 'delete'">
            <el-button type="text" @click="editUser(scope.row.id)" size="small"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="handleDelete(scope.row.id)"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[15, 30, 45, 60]"
          :page-size="shopFrom.limit"
          :current-page="shopFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--编辑-->
    <el-dialog
      :title="`${id ? '编辑' : '新增'}门店`"
      :visible.sync="visible"
      width="600px"
    >
      <edit-from v-if="visible" :id="id" @resetForm="resetForm"></edit-from>
    </el-dialog>
  </div>
</template>

<script>
import { shopListApi, shopUpdateStatusApi, shopDeleteApi } from "@/api/user";
import editFrom from "./edit";

export default {
  name: "ShopIndex",
  components: { editFrom },
  data() {
    return {
      headeTab: [
        { type: "show", name: "显示中的门店" },
        { type: "hidden", name: "隐藏中的门店" },
        { type: "delete", name: "回收站的门店" }
      ],
      tab: "show",
      tableData: {
        data: [],
        total: 0
      },
      loading: true,
      shopFrom: {
        name: "",
        page: 1,
        limit: 15
      },
      visible: false,
      id: 0
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    resetForm() {
      this.visible = false;
    },
    reset(formName) {
      this.shopFrom = {
        name: "",
        page: 1,
        limit: 15
      };
      this.getList();
    },
    editUser(id) {
      this.id = id;
      this.visible = true;
    },
    // 搜索
    searchs() {
      this.shopFrom.page = 1;
      this.getList();
    },
    // 列表
    getList(num) {
      this.loading = true;
      this.shopFrom.page = num ? num : this.shopFrom.page;
      if (this.tab === "show") {
        this.shopFrom.status = 1;
        this.shopFrom.isDel = 0;
      }
      if (this.tab === "hidden") {
        this.shopFrom.status = 0;
        this.shopFrom.isDel = 0;
      }
      if (this.tab === "delete") {
        this.shopFrom.status = undefined;
        this.shopFrom.isDel = 1;
      }
      if (this.tab == 0) this.shopFrom.shopType = "";
      shopListApi(this.shopFrom)
        .then(res => {
          this.tableData.data = res;
          this.tableData.total = 15;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    pageChange(page) {
      this.shopFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.shopFrom.limit = val;
      this.getList();
    },
    onchangeIsShow(row) {
      shopUpdateStatusApi({ id: row.id, status: row.status }).then(() => {
        this.$message.success("操作成功");
        this.getList();
      });
    },
    // 删除
    handleDelete(id) {
      this.$modalSure("确定要删除该店铺吗？").then(() => {
        shopDeleteApi({ id }).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.text-right {
  text-align: right;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 33.33%;
}
.selWidth {
  width: 100% !important;
}
.seachTiele {
  line-height: 30px;
}
.container {
  min-width: 821px;
  /deep/.el-form-item {
    width: 100%;
  }
  /deep/.el-form-item__content {
    width: 72%;
  }
}
.ivu-ml-8 {
  font-size: 12px;
  color: #1682e6;
}
</style>
